package com.xgluo;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;

/**
 * @author xgluo
 * @program: makePackage
 * @name: ReadConfigFile
 * @description: 读取配制文件
 * @date 2025年02月28日
 */
public class ReadConfigFile {

    private static ReadConfigFile instance;
    private final String projectPath;
    private final String classPath;
    private final String webcontextPath;

    private ReadConfigFile(String filePath) {
        //定义一个解析工具
        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = Files.newInputStream(Paths.get(filePath));
            properties.load(inputStream);
            projectPath = properties.getProperty("ProjectPath");
            classPath = properties.getProperty("ClassPath");
            webcontextPath = properties.getProperty("WebContextPath");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static ReadConfigFile getInstance(String filePath) {
        if(instance == null) {
            synchronized (ReadConfigFile.class) {
                if(instance == null) {
                    instance = new ReadConfigFile(filePath);
                }
            }
        }
        System.out.println("projectPath: "+instance.getProjectPath());
        System.out.println("classPath: "+instance.getClassPath());
        System.out.println("webcontextPath: "+instance.getWebcontextPath());
        return instance;
    }


    public String getProjectPath() {
        return projectPath;
    }

    public String getClassPath() {
        return classPath;
    }

    public String getWebcontextPath() {
        return webcontextPath;
    }

    public static ReadConfigFile getInstance() {
        return instance;
    }
}
